<?php
/*
+--------------------------------------------------------------------------
|   AffiliStore 2
|   ========================================
|   Web: http://www.affilistore.com
|   Email: admin (at) affilistore (dot) com
|	License Type: AffiliStore 2 is NOT open source software and limitations apply 
|   Licence Info: Visit AffiliStore website and click on 'Licence'
+--------------------------------------------------------------------------
*/
set_include_path(PATH_SEPARATOR . dirname(dirname(dirname(realpath(__FILE__)))) . '/' . 'classes/' .
				 PATH_SEPARATOR . dirname(dirname(dirname(realpath(__FILE__)))) . '/' . 
				 PATH_SEPARATOR . dirname(realpath(__FILE__)) . '/' . 
				 PATH_SEPARATOR . get_include_path() );

require_once('../../includes/connect.php');
require_once('../../includes/functions.php');

mysql_select_db($database, $databaseConnect);

if (isset($_GET['fd_refresh'])) {


$fdrefresh = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'feedMemory".$_GET['fd_refresh']."'");
$refresh = mysql_fetch_assoc($fdrefresh);

$pieces = explode("-:-", $refresh['value']);
	
$fdURLfeed = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'csvURL".$_GET['fd_refresh']."'");
$URLfeed = mysql_fetch_assoc($fdURLfeed);
$fdtypeFeed = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'csvType".$_GET['fd_refresh']."'");
$typeFeed = mysql_fetch_assoc($fdtypeFeed);

	if ($typeFeed['value'] == 'tab') {
	$typeFeed = "\t";
	} else {
	$typeFeed = $typeFeed['value'];
	}

// set row variable and open file
$row = intval($pieces[11]);
if ($URLfeed['title'] == 'Upload Feed') {
$handle = fopen('../'.$URLfeed['value'], "r");
} else {
$handle = fopen($URLfeed['value'], "r");
}
// empty the table
$empty = "DELETE FROM affiliSt_products1 WHERE prodDB IN ('".$_GET['fd_refresh']."')";
mysql_query($empty);

// start auto inc from last ID in db
$autoinc = mysql_query("SELECT prodID FROM affiliSt_products1 ORDER BY prodDB DESC LIMIT 1");
$autoincnum = mysql_fetch_assoc($autoinc);
$resetautoinc = mysql_query("ALTER TABLE affiliSt_products1 AUTO_INCREMENT = ".($autoincnum['prodID']+1)."");

// while loop with fgetcsv sorts the csv into the data array 
while (($data = fgetcsv($handle, 3000, $typeFeed)) !== FALSE) {

	$resulta = $pieces[0];
	if ($data[intval($pieces[1])] == NULL) {
	$resultb = $row;
	} else {
	$resultb = $data[intval($pieces[1])];
	}
	$resultc = ucwords(strtolower($data[intval($pieces[2])]));
	$resultd = ucwords(strtolower($data[intval($pieces[3])]));
	$resulte = ucwords(strtolower($data[intval($pieces[4])]));
	$resultf = $data[intval($pieces[5])];
	$resultg = $data[intval($pieces[6])];
	$resulth = $data[intval($pieces[7])];
	$resulti = $data[intval($pieces[8])];
	$resultj = $data[intval($pieces[9])];
	$resultk = $pieces[10];
	$resultm = $data[intval($pieces[14])];
	$resultn = $data[intval($pieces[15])];
	$resulto = $data[intval($pieces[16])];
	$resultp = $data[intval($pieces[17])];
	$resultq = $data[intval($pieces[18])];
	$resultr = $data[intval($pieces[34])];
	$results = intval($row);
	
	if ($pieces[12] == 'before') {
	$resulth = $pieces[13].$resulth;
	} else if ($pieces[12] == 'after') {
	$resulth = $resulth.$pieces[13];
	}

// remove characters that may interfear with navigation or display
	include("../../includes/fixlist.inc.php");
	
	$resultc = str_replace($andAmps, "and", $resultc);
	$resultd = str_replace($andAmps, "and", $resultd);
	$resulte = str_replace($andAmps, "and", $resulte);
	$resultc = str_replace($charquotes, "", $resultc);
	$resultd = str_replace($charquotes, "", $resultd);
	$resulte = str_replace($charquotes, "", $resulte);
	$resultb = str_replace($allCharacters, "-", $resultb);
	$resultc = str_replace($allCharacters, " ", $resultc);
	$resultd = str_replace($someCharacters, " ", $resultd);
	$resulte = str_replace($allCharacters, " ", $resulte);
	$resultj = str_replace($currencyCharacters, " ", $resultj);
	$resultl = $_GET['fd_refresh'];

// miss out headers row
   if ($row != 0) {
   // insert all the data into the database table
		$sql = sprintf("INSERT INTO affiliSt_products1 (
	  
	  			merchant,	
				merchantProdID, 	
				prodCategory,	
				prodName, 	
				prodBrand, 	
				prodDescription, 	
				prodPromoText, 	
				prodLink, 	
				prodImageURL, 	
				prodPrice, 
				prodCurrency,
				prodDB,
				extraFieldA,
				extraFieldB,
				extraFieldC,
				extraFieldD,
				extraFieldE, 	
				prodImageSmall, 	
				dbProdID
				
				) VALUES (
				
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s,
				%s
				)",
				quote_smart($resulta),
				quote_smart($resultb),
				quote_smart($resultc),
				quote_smart($resultd),
				quote_smart($resulte),
				quote_smart($resultf),
				quote_smart($resultg),
				quote_smart($resulth),
				quote_smart($resulti),
				quote_smart($resultj),
				quote_smart($resultk),
				quote_smart($resultl),
				quote_smart($resultm),
				quote_smart($resultn),
				quote_smart($resulto),
				quote_smart($resultp),
				quote_smart($resultq),
				quote_smart($resultr),
				quote_smart($results));
		mysql_query($sql, $databaseConnect) or die(mysql_error());
		}
	
	$row++;
	}
	
// create the new config nav
// category listing
$getCategory = mysql_query("SELECT prodCategory FROM affiliSt_products1 WHERE prodDB = '".$_GET['fd_refresh']."' ORDER BY prodCategory ASC");
$categoryList = mysql_fetch_assoc($getCategory);
$newnavigation = '';
do { 
$catresult = ucwords(strtolower($categoryList['prodCategory']));
	if ($storedCategory != $catresult && $catresult != NULL) {
	$newnavigation .= $catresult;
	// start sub nav
	// brand listing
	$getBrands = mysql_query("SELECT prodBrand FROM affiliSt_products1 WHERE prodDB = '".$_GET['fd_refresh']."' AND prodCategory = '$catresult' ORDER BY prodBrand ASC");
	$brandList = mysql_fetch_assoc($getBrands);
		do {
		$theresult = ucwords(strtolower($brandList['prodBrand']));
			if ($storedBrand != $theresult && $theresult != NULL) {
			$newnavigation .= '(:)'.$theresult;
			$storedBrand = $theresult;
			$brandrow++;
			} else {
			$theresult = ucwords(strtolower($brandList['prodBrand']));
			}
		} while ($brandList = mysql_fetch_assoc($getBrands));
	$storedCategory = $catresult;
	$newnavigation .= '-:-';
	} else {
	$catresult = ucwords(strtolower($categoryList['prodCategory']));
	}
} while ($categoryList = mysql_fetch_assoc($getCategory));
$newnavigation = rtrim($newnavigation, '-:-');

$sql= "DELETE FROM affiliSt_config WHERE name='nav".$_GET['fd_refresh']."'" ;        
mysql_query($sql, $databaseConnect) or die(mysql_error());
$insert = array (
		array ( 'name' =>  'nav'.$_GET['fd_refresh'],
				'value' => $newnavigation,
				'comment' => 'Category and subcategory navigation',
				'title' => 'Generated Navigation')
);
	
// insert values posted from Step 1 into config table
foreach ($insert as $row) {
	$sql = sprintf("INSERT INTO affiliSt_config (
			name, value, comment, title
			) values (
			%s, %s, %s, %s
			)",
			quote_smart($row['name']),
			quote_smart($row['value']),
			quote_smart($row['comment']),
			quote_smart($row['title']));
	mysql_query($sql, $databaseConnect) or die(mysql_error());
}
// end config nav



// get config details
$getMyEmail = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'adEmail'");
$myEmail = mysql_fetch_assoc($getMyEmail);

// new link email
$myemailaddress = $myEmail['value'];
$emailaddress = $myemailaddress;
$subject = $pieces[0]." Cron Just Updated";
$emailmessage = $emailNew['value'];
$emailfrom = $myemailaddress;
$messageb .= "Dear Admin\n\n".$pieces[0]." cron just updated"."\n\n";
$runtime = (date("d M Y H:i"));
$messageb .= "Time of the message: $runtime (server time zone)\n\n";
mail($emailaddress, $subject, $messageb, "From: $emailfrom");

}
?>